package com.letoy.study.service.Impl;

import com.letoy.study.config.StoreConfig;
import com.letoy.study.dao.CourseMapper;
import com.letoy.study.entity.CourseInfo;
import com.letoy.study.service.CourseService;
import com.letoy.study.utils.ConvertUtils;
import com.letoy.study.utils.FileUploadUtil;
import com.letoy.study.vo.CourseVo;
import com.letoy.study.vo.VideoUpload;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;


import static com.letoy.study.utils.Constants.FAIL;
import static com.letoy.study.utils.Constants.SUCCESS;

/**
 * @program:letoy-study-java
 * @author: WeiHaoL
 * @Time: 2021/1/25  下午3:04
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Resource
    CourseMapper courseMapper;


    private final StoreConfig storeConfig;

    public CourseServiceImpl(StoreConfig storeConfig) {
        this.storeConfig = storeConfig;
    }


    @Override
    public HashMap<String, Object> releaseCourse(CourseVo courseVo) {

        HashMap<String, Object> hashMap = new HashMap<>();

        // 使用Spring BeanUtils拷贝Vo到新创建的courserInfo
        CourseInfo courseInfo = new CourseInfo();
        BeanUtils.copyProperties(courseVo, courseInfo);

        // 使用工具类转换时间 将string转换为java.sql.timestamp
        String coursePublishTime = courseVo.getCoursePublishTime();
        Timestamp timestamp = ConvertUtils.strToSqlDate(coursePublishTime, "yyyy-MM-dd HH:mm:ss");
        courseInfo.setCoursePublishTime(timestamp);


        System.out.println(courseInfo);


        int i = courseMapper.releaseCourse(courseInfo);
        if (i == 0) {
            hashMap.put("status", FAIL);
            hashMap.put("msg", "发布失败");
            return hashMap;
        }
        hashMap.put("status", SUCCESS);
        hashMap.put("msg", "发布成功");
        return hashMap;
    }

    @Override
    public HashMap<String, Object> deleteCourse(CourseInfo courseInfo) {

        HashMap<String, Object> hashMap = new HashMap<>();

        int i = courseMapper.deleteCourse(courseInfo);
        if (i == 0) {
            hashMap.put("status", FAIL);
            hashMap.put("msg", "删除失败");
            return hashMap;
        }
        hashMap.put("status", SUCCESS);
        hashMap.put("msg", "删除成功");
        return hashMap;
    }

    @Override
    public HashMap<String, Object> findAllCourseInfo() {

        HashMap<String, Object> hashMap = new HashMap<>();

        List<CourseInfo> allCourseInfoByCourseType = courseMapper.findAllCourseInfoByCourseType("");

        if (allCourseInfoByCourseType == null) {
            hashMap.put("status", FAIL);
            hashMap.put("msg", "查询失败");
            return hashMap;
        }
        hashMap.put("status", SUCCESS);
        hashMap.put("msg", "查询成功");
        hashMap.put("data", allCourseInfoByCourseType);
        return hashMap;
    }

    @Override
    public HashMap<String, Object> findAllCourseInfo(String courseType) {
        HashMap<String, Object> hashMap = new HashMap<>();

        List<CourseInfo> allCourseInfoByCourseType = courseMapper.findAllCourseInfoByCourseType(courseType);

        if (allCourseInfoByCourseType == null) {
            hashMap.put("status", FAIL);
            hashMap.put("msg", "查询失败");
            return hashMap;
        }
        hashMap.put("status", SUCCESS);
        hashMap.put("msg", "查询成功");
        hashMap.put("data", allCourseInfoByCourseType);
        return hashMap;
    }

    @Override
    public HashMap<String, Object> videoUpload(VideoUpload videoUpload) {

        HashMap<String, Object> hashMap = new HashMap<>();

        int i = courseMapper.updateVideoUrl(videoUpload.getCourseId(),"/video/" + videoUpload.getFile().getOriginalFilename());
        boolean b = FileUploadUtil.uploadFile(videoUpload.getFile(),storeConfig.getFilePath());
        if (i == 0 || !b) {
            hashMap.put("status", FAIL);
            hashMap.put("msg", "更新失败");
            return hashMap;
        }
        hashMap.put("status", SUCCESS);
        hashMap.put("msg", "更新成功");
        return hashMap;
    }
}
